package com.jia.lidao.others.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.reflect.TypeToken;
import com.jia.lidao.R;
import com.jia.lidao.RunningContext;
import com.jia.lidao.UserUtils;
import com.jia.lidao.enity.UploaderResponse;
import com.jia.lidao.others.API;
import com.jia.lidao.others.BaseFragment;
import com.jia.lidao.others.enity.ObjectCallBack;
import com.jia.lidao.utils.LogUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;
import okhttp3.Call;

import static com.jia.lidao.RunningContext.IMAGE_REQUEST_CODE;


/**
 * Created by 00029935 on 2017/12/11.
 */

public class OffLinePayFragment extends BaseFragment {
    @BindView(R.id.ib_back)
    ImageView ibBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.title_view)
    RelativeLayout titleView;
    @BindView(R.id.iv_pic)
    ImageView ivPic;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    Unbinder unbinder;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_order_cash)
    TextView tvOrderCash;
    @BindView(R.id.loading)
    ProgressBar loading;
    private String pic = "";
    private double cash;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        cash = getActivity().getIntent().getDoubleExtra("cash", 0);
    }

    @Override
    protected int getInflateLayout() {
        return R.layout.nong_zi_pay_fragment;
    }

    @Override
    protected void setUpView(View view) {
        tvTitle.setText("填写订货单");
        tvOrderCash.setText("订单金额  " + cash);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.ib_back, R.id.iv_pic, R.id.tv_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ib_back:
                getActivity().finish();
                break;
            case R.id.iv_pic:
                initgetPhotoview();
                break;
            case R.id.tv_commit:
                if (!TextUtils.isEmpty(pic)) {
                    Intent intent = new Intent();
                    intent.putExtra("value", pic);
                    getActivity().setResult(Activity.RESULT_OK, intent);
                    getActivity().finish();
                }else {
                    showToast("请上传打款凭证图片");
                }
                break;
        }
    }


    private void initgetPhotoview() {
        Intent intent = new Intent(getContext(), MultiImageSelectorActivity.class);
        // whether show camera
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
        // max select image amount
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 1);
        // select mode (MultiImageSelectorActivity.MODE_SINGLE OR MultiImageSelectorActivity.MODE_MULTI)
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_MULTI);
        startActivityForResult(intent, IMAGE_REQUEST_CODE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case IMAGE_REQUEST_CODE:

                ArrayList<String> selectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                if (selectPath != null && selectPath.size() > 0) {
                    for (int i = 0; i < selectPath.size(); ++i) {

                        Glide.with(getContext()).load(selectPath.get(i)).into(ivPic);
                        uploadPic(selectPath.get(i));
                    }
                }
        }
    }

    private void uploadPic(String picurl) {
        loading.setVisibility(View.VISIBLE);
        String url = API.UPLOAD + UserUtils.getInstance().getAuthor().access_token;
        HashMap<String, String> heades = new HashMap<>();
        heades.put("Accept", "*/*");
        TypeToken<UploaderResponse> typeToken = new TypeToken<UploaderResponse>() {
        };
        File file = new File(picurl);
        OkHttpUtils.post()//
                .addFile("mFile", file.getName(), file)
                .url(url)
                .headers(heades)//
                .build()//
                .execute(new ObjectCallBack<UploaderResponse>(typeToken) {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(UploaderResponse response, int id) {
                        if (response.getErr_code() == 0) {
                            pic = response.getResult().getMFile();

                        }
                        loading.setVisibility(View.GONE);
                    }
                });
    }

    @OnClick(R.id.tv_right)
    public void onClick() {


    }
}
